package cn.iocoder.yudao.module.call.controller.admin.ivrvoice;

import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.call.controller.admin.ivrflow.vo.IvrFlowNodeVO;
import cn.iocoder.yudao.module.call.controller.admin.ivrvoice.vo.IvrVoicePageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.ivrvoice.vo.IvrVoiceRespVO;
import cn.iocoder.yudao.module.call.controller.admin.ivrvoice.vo.IvrVoiceSaveReqVO;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrvoice.IvrVoiceDO;
import cn.iocoder.yudao.module.call.service.ivrvoice.IvrVoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.sound.sampled.*;
import javax.validation.Valid;
import java.io.*;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - ivr-语音")
@RestController
@RequestMapping("/call/ivr-voice")
@Validated
public class IvrVoiceController {

    @Resource
    private IvrVoiceService ivrVoiceService;


    @PostMapping("/createVoiceList")
    @Operation(summary = "批量生成话术语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createVoiceList(@RequestBody IvrVoiceSaveReqVO createReqVO) {
        Long voiceList = ivrVoiceService.createVoiceList(createReqVO);
        return success(voiceList);
    }

    @PostMapping("/createVoiceByNodeId")
    @Operation(summary = "根据nodeId生成话术语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createVoiceByNodeId(@RequestBody IvrVoiceSaveReqVO createReqVO) {
        Long voiceList = ivrVoiceService.createVoiceByNodeId(createReqVO);
        return success(voiceList);
    }

    @PostMapping("/createVoiceByNodeIdManual")
    @Operation(summary = "根据nodeId手动上传话术语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<String> createVoiceByNodeIdManual(@RequestParam("file") MultipartFile file,
                                                        @RequestParam("flowId") Long flowId,
                                                        @RequestParam("nodeId") String nodeId) {
        IvrVoiceSaveReqVO createReqVO = new IvrVoiceSaveReqVO();
        createReqVO.setFile(file);
        createReqVO.setFlowId(flowId);
        createReqVO.setNodeId(nodeId);
        Long voiceList = ivrVoiceService.createVoiceByNodeIdManual(createReqVO);
        return success("操作成功");
    }


    @GetMapping("/delVoiceList")
    @Operation(summary = "批量删除话术语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> delVoiceList(@RequestParam("flowId") Long flowId) {
        Long voiceList = ivrVoiceService.delVoiceList(flowId);
        return success(voiceList);
    }

    @PutMapping("/upDateIvrContentByNodeId")
    @Operation(summary = "根据节点id删除话术语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> upDateIvrContentByNodeId(@RequestBody IvrFlowNodeVO ivrFlowNodeVO) {
        Long voiceList = ivrVoiceService.upDateIvrContentByNodeId(ivrFlowNodeVO);
        return success(voiceList);
    }
    @GetMapping("/delVoiceByNodeId")
    @Operation(summary = "根据节点id删除话术语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> delVoiceByNodeId(@RequestParam("flowId") Long flowId,@RequestParam("nodeId") String  nodeId) {
        Long voiceList = ivrVoiceService.delVoiceByNodeId(flowId,nodeId);
        return success(voiceList);
    }


    @GetMapping("/ivrContentList")
    @Operation(summary = "话术列表")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<List<JSONObject>> ivrContentList(@RequestParam("flowId") Long flowId) {
        List<JSONObject> list = ivrVoiceService.ivrContentList(flowId);
        return success(list);
    }
    @PostMapping("/audition")
    @Operation(summary = "试听")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<IvrVoiceSaveReqVO> audition(@RequestBody IvrVoiceSaveReqVO createReqVO) {
        IvrVoiceSaveReqVO audition = ivrVoiceService.audition(createReqVO);
        return success(audition);
    }

    @PostMapping("/create")
    @Operation(summary = "创建ivr-语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<Long> createIvrVoice(@RequestBody IvrVoiceSaveReqVO createReqVO) throws Exception {
        return success(ivrVoiceService.createIvrVoice(createReqVO));
    }



    @PostMapping("/oneKeyCreate")
    @Operation(summary = "试听")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:create')")
    public CommonResult<IvrVoiceSaveReqVO> oneKeyCreate(@RequestBody IvrVoiceSaveReqVO createReqVO) {
//        List<RobotAgentDO> agentList = TenantUtils.execute(tenantId, () -> robotAgentService.getAgentList());
        IvrVoiceSaveReqVO audition = ivrVoiceService.audition(createReqVO);
        return success(audition);
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr-语音")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:update')")
    public CommonResult<Boolean> updateIvrVoice(@Valid @RequestBody IvrVoiceSaveReqVO updateReqVO) {
        ivrVoiceService.updateIvrVoice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr-语音")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:delete')")
    public CommonResult<Boolean> deleteIvrVoice(@RequestParam("id") Long id) {
        ivrVoiceService.deleteIvrVoice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr-语音")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<IvrVoiceRespVO> getIvrVoice(@RequestParam("id") Long id) {
        IvrVoiceDO ivrVoice = ivrVoiceService.getIvrVoice(id);
        return success(BeanUtils.toBean(ivrVoice, IvrVoiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr-语音分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<PageResult<IvrVoiceRespVO>> getIvrVoicePage(@Valid IvrVoicePageReqVO pageReqVO) {
        PageResult<IvrVoiceDO> pageResult = ivrVoiceService.getIvrVoiceList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IvrVoiceRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得ivr-语音分页")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:query')")
    public CommonResult<List<IvrVoiceDO>> getIvrVoiceList() {
        List<IvrVoiceDO> pageResult = ivrVoiceService.getIvrVoiceList();
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr-语音 Excel")
    @PreAuthorize("@ss.hasPermission('call:ivr-flow:export')")
    @OperateLog(type = EXPORT)
    public void exportIvrVoiceExcel(@Valid IvrVoicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IvrVoiceDO> list = ivrVoiceService.getIvrVoiceList(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr-语音.xls", "数据", IvrVoiceRespVO.class,
                        BeanUtils.toBean(list, IvrVoiceRespVO.class));
    }

}